﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KeuanganEdit : Form
    {
        private List<int> cat_id = new List<int>();
        private Keuangan parent = null;
        private DataTable dTable = null;
        private int rowIndex = -1;

        public KeuanganEdit()
        {
            InitializeComponent();
        }

        private enum Category { PEMASUKAN, PENGELUARAN }
        public KeuanganEdit(Keuangan parent_instance, DataTable finance_dataTable, int editRowIndex,
            string finance_receipt,
            string finance_type,
            object finance_expense_id,
            object finance_income_id,
            string finance_description,
            object finance_amount)
        {
            InitializeComponent();

            if (finance_type == "PENGELUARAN")
            {
                cmbTipe.SelectedIndex = 0;
                loadKategori(Category.PENGELUARAN);
                for (int i = 0; i < cat_id.Count; i++)
                {
                    if (finance_expense_id.ToString().Length == 0)
                    {
                        // Do nothing
                    }
                    else
                    {
                        if (cat_id[i] == (int)finance_expense_id)
                        {
                            cmbKategori.SelectedIndex = i;
                            break;
                        }
                    }
                }  
            }
            else if (finance_type == "PEMASUKAN")
            {
                cmbTipe.SelectedIndex = 1;
                loadKategori(Category.PEMASUKAN);
                for (int i = 0; i < cat_id.Count; i++)
                {
                    if (finance_income_id.ToString().Length == 0)
                    {
                        // Do nothing
                    }
                    else
                    {
                        if (cat_id[i] == (int)finance_income_id)
                        {
                            cmbKategori.SelectedIndex = i;
                            break;
                        }
                    }

                    
                }  
            }

            this.parent = parent_instance;
            this.dTable = finance_dataTable;
            this.rowIndex = editRowIndex;

            this.receipt.Text = finance_receipt;
            this.description.Text = finance_description;

            decimal value = (decimal)finance_amount;
            int value_int = Convert.ToInt32(value);

            this.amount.Text = value_int.ToString();
        }

        private void KeuanganEdit_Load(object sender, EventArgs e)
        {

        }

        private void loadKategori(Category category)
        {
            string sql;
            if (category == Category.PENGELUARAN)
                sql = "SELECT * FROM Expense_type";
            else
                sql = "SELECT * FROM Income_type WHERE id <> 1";

            cat_id.Clear();
            cmbKategori.Items.Clear();
            DataTable table = Database.getInstance().query(sql);
      
            foreach (DataRow row in table.Rows)
            {
                cat_id.Add((int)row[0]);
                cmbKategori.Items.Add(row[1].ToString());
                // Display selected index
                cmbKategori.SelectedIndex = 0;
            }
        }

        private void cmbTipe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbTipe.SelectedIndex == 0)
                loadKategori(Category.PENGELUARAN);
            else if (cmbTipe.SelectedIndex == 1)
                loadKategori(Category.PEMASUKAN);
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private Boolean checkFieldValue()
        {
            if (this.description.TextLength == 0)
            {
                MessageBox.Show("Harap masukkan data keterangan !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (cmbTipe.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih kategori !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.amount.Text == "")
            {
                MessageBox.Show("Harap masukkan data jumlah !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            return true;
        }

        private void amount_TextChanged(object sender, EventArgs e)
        {
            TextBox txtInput = (TextBox)sender;

            if (txtInput.TextLength != 0)
            {
                for (int i = 0; i < txtInput.TextLength; i++)
                {
                    if (Char.IsDigit(txtInput.Text, i))
                    {
                        // Do not do anything
                    }
                    else
                    {     
                        MessageBox.Show("Harap gunakan angka !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                    }
                }
            }    
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (true == this.checkFieldValue())
            {
                DataTable dt = Database.getInstance().query("select * from Finance");

                // Edit DataTable
                dt.Rows[this.rowIndex]["receipt_no"] = this.receipt.Text;
                dt.Rows[this.rowIndex]["time"] = this.dateTransaction.Value;
                dt.Rows[this.rowIndex]["description"] = this.description.Text;
                dt.Rows[this.rowIndex]["amount"] = decimal.Parse(this.amount.Text);

                if (this.cmbTipe.SelectedIndex == 0)
                {
                    dt.Rows[this.rowIndex]["type"] = "PENGELUARAN";

                    if (this.cmbKategori.Items.Count > 0)
                    {
                        dt.Rows[this.rowIndex]["expense_id"] = this.cat_id[this.cmbKategori.SelectedIndex];
                    }
                    dt.Rows[this.rowIndex]["income_id"] = DBNull.Value;
                }
                else if (this.cmbTipe.SelectedIndex == 1)
                {
                    dt.Rows[this.rowIndex]["type"] = "PEMASUKAN";
                    
                    if (this.cmbKategori.Items.Count > 0)
                    {
                        dt.Rows[this.rowIndex]["income_id"] = this.cat_id[this.cmbKategori.SelectedIndex];      
                    }
                    dt.Rows[this.rowIndex]["expense_id"] = DBNull.Value;
                }

                // Update Database
                Database.getInstance().update(dt);
                this.parent.refreshTableData();

                if (this.cmbTipe.SelectedIndex == 0)
                {
                    var result = MessageBox.Show("Edit pengeluaran berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        // Close form
                        this.Close();
                    }
                }
                else if (this.cmbTipe.SelectedIndex == 1)
                {
                    var result = MessageBox.Show("Edit pemasukan berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        // Close form
                        this.Close();
                    }
                }   
            }
        }
    }
}
